<?php

use GeoIp2\Database\Reader;
use org\myTemplate;
use think\Env;
use Zhuzhichao\IpLocationZh\Ip;

// use UAParser\Parser;



function getIpInfo($ip){
    if(!$ip || $ip == '127.0.0.1'){return ['city'=>'','province'=>''];}

    $ipinfo  = Ip::find($ip);
    if($ipinfo[1] == "局域网"){
        $ipinfo[1] = "";
    }
    return ['city'=>$ipinfo[2],'province'=>$ipinfo[1]];
}



function getRealIp(){
  $ip=false;
  if(!empty($_SERVER["HTTP_CLIENT_IP"])){
    $ip = $_SERVER["HTTP_CLIENT_IP"];
  }
  if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
    if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
    for ($i = 0; $i < count($ips); $i++) {
      if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
        $ip = $ips[$i];
        break;
      }
    }
  }
  return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}


/**
 * 按照设定的时间组判断当前是否启用
 */
function checkTime($times){
    $now_time = date('H',time());
    if(is_array($times)){
        if (in_array($now_time,$times)) {
            return true;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

/**
 * 返回搜索关键词
 */
function getKeyWord($url){
    $query = parse_url($url,PHP_URL_QUERY);
    parse_str($query,$query_arr);
    // keyword word q
    if(isset($query_arr['keyword'])){
        return $query_arr['keyword'];
    }
    if(isset($query_arr['word'])){
        return $query_arr['word'];
    }
    if(isset($query_arr['q'])){
        return $query_arr['q'];
    }
}

/**
 * 获取搜索引擎类型  1搜狗 2百度 3神马
 */
function getSearchType($url){
    $host = parse_url($url, PHP_URL_HOST);
    $search_lists = ['sogou.com', 'baidu.com', 'sm.cn'];
    for ($i = 0; $i < count($search_lists); $i++) {
		if (strpos( strtolower($url), $search_lists[$i]) !== false ) {
			return $i+1;
		}
	}
}

/**
 * 关键词处理
 * @param $swt_msg
 * @param $data
 * @param $info
 * @return mixed
 */
function keyword_replace($swt_msg,$wd){
    if (strpos($swt_msg,"[keyword]") !== false){
        return str_replace("[keyword]",'<em>'.$wd."</em>",$swt_msg);
    }

    return $swt_msg;
}

/**
 * 
 * @param mixed $paths 
 * @param mixed $res 
 * @param mixed $path 
 * @param mixed $doctor_pic 
 * @return array 
 */
function get_file_json($paths){
    $res = [];

    foreach($paths as $k => $path){
        $doctor_pic = pathinfo($path);
        if( strpos($path,"http://") === false){
            $path = Env::get('cdn_host').$path;    
        }

        $res[$k] = [
            "key" => $doctor_pic["basename"],
            "value" => $path
        ];
    }

    return $res;
}